package com.wx.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wx.common.JsonArrayResult;
import com.wx.common.JsonObjectResult;
import com.wx.common.redis.RedisCacheHand;
import com.wx.common.redis.RedisUtil;
import com.wx.entity.GoodsPrice;
import com.wx.entity.IndexBanner;
import com.wx.entity.IndexCategory;
import com.wx.entity.IndexGoods;
import com.wx.entity.IndexNotice;
import com.wx.entity.IndexNoticeDetail;
import com.wx.service.ShopService;

@RestController
@RequestMapping("/api/shop")
public class ShopController {

	@Autowired
	private ShopService shopService;
	
	@Autowired
	private RedisUtil redis;

	// 请求主页公告信息
	@GetMapping("/notice/notice_list")
	@ResponseBody
	public JsonArrayResult get_all_notice() {
		List<IndexNotice> list = new ArrayList<IndexNotice>();
		list = shopService.selectAllNotice();
		JsonArrayResult result = new JsonArrayResult(JSONArray.parseArray(JSON.toJSONString(list)));
		return result;
	}

	// 请求主页公告详细内容
	@GetMapping("/notice/notice_detail")
	@ResponseBody
	public JsonObjectResult get_notice_detail(@RequestParam(value = "id", required = false) Integer id) {
		IndexNoticeDetail item = new IndexNoticeDetail();
		item = shopService.findNoticeDetail(id);
		JsonObjectResult result = new JsonObjectResult(JSONObject.parseObject(JSON.toJSONString(item)));
		return result;
	}

	// 请求主页分类标签列表
	@GetMapping("/category/category_list")
	@ResponseBody
	public JsonArrayResult get_all_category() {
		List<IndexCategory> list = new ArrayList<IndexCategory>();
		list = shopService.selectAllCategory();
		JsonArrayResult result = new JsonArrayResult(JSONArray.parseArray(JSON.toJSONString(list)));
		return result;
	}

	// 请求主页广告轮播列表
	@GetMapping("/banner/banner_list")
	@ResponseBody
	public JsonArrayResult get_all_banner() {
		List<IndexBanner> list = new ArrayList<IndexBanner>();
		list = shopService.selectAllBanner();
		JsonArrayResult result = new JsonArrayResult(JSONArray.parseArray(JSON.toJSONString(list)));
		return result;
	}

	/**
	 * 请求商品列表
	 * @param recommendStatus 分类（推荐、普通）
	 * @param categoryId 分类
	 * @param nameLike 搜索
	 * @param page 分页
	 * @param pageSize 每页条数
	 * @param pintuan 
	 * @return
	 */
	@PostMapping("/goods/goods_list")
	@ResponseBody
	public JsonArrayResult get_goods_by_recommendStatus(
			@RequestParam(value = "recommendStatus", required = false) Integer recommendStatus,
			@RequestParam(value = "categoryId", required = false) Integer categoryId,
			@RequestParam(value = "nameLike", required = false) String nameLike,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "pintuan", required = false) Boolean pintuan) {
		List<IndexGoods> list = new ArrayList<IndexGoods>();
		list = shopService.selectByRecommendStatus(recommendStatus,categoryId,nameLike,page,pageSize,pintuan);
		JsonArrayResult result = new JsonArrayResult(JSONArray.parseArray(JSON.toJSONString(list)));
		return result;
	}
	
	/**
	 * 根据所选规格查询商品价格
	 * @param goodsId 商品id
	 * @param propertyChildIds 规格编码
	 * @return
	 */
	@PostMapping("/goods/price")
	@ResponseBody
	public JsonObjectResult get_goods_price(
			@RequestParam(value = "goodsId") Integer goodsId,
			@RequestParam(value = "propertyChildIds") String propertyChildIds) {
		GoodsPrice price = new GoodsPrice();
		price = JSON.parseObject(redis.get(RedisCacheHand.GoodsPriceCache + goodsId + ":" + propertyChildIds), GoodsPrice.class);
		JsonObjectResult result = new JsonObjectResult(JSONObject.parseObject(JSON.toJSONString(price)));
		return result;
	}
	
}
